package com.fsh.subject.domain.handler.subject;

import com.fsh.subject.common.enums.IsDeletedFlagEnum;
import com.fsh.subject.common.enums.SubjectInfoTypeEnum;
import com.fsh.subject.domain.convert.JudgeSubjectConverter;
import com.fsh.subject.domain.entity.SubjectAnswerBO;
import com.fsh.subject.domain.entity.SubjectInfoBO;
import com.fsh.subject.domain.entity.SubjectOptionBO;
import com.fsh.subject.infra.basic.entity.SubjectJudge;
import com.fsh.subject.infra.basic.service.SubjectJudgeService;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 判断题目的策略类
 *
 * @author: ChickenWing
 * @date: 2023/10/5
 */
@Component
public class JudgeTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectJudgeService subjectJudgeService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        Preconditions.checkNotNull(subjectInfoBO.getOptionList(),"选项不能为空");
        Preconditions.checkArgument(!subjectInfoBO.getOptionList().isEmpty(),"没有对应的选项");

        SubjectJudge subjectJudge=new SubjectJudge();
        SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);
        subjectJudge.setSubjectId(subjectInfoBO.getId());
        subjectJudge.setIsCorrect(subjectAnswerBO.getIsCorrect());
        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectJudgeService.insert(subjectJudge);
    }

    /**
     * 查询判断题
     */
    @Override
    public SubjectOptionBO query(Long subjectId) {
        //查询  判断表
        SubjectJudge subjectJudge=new SubjectJudge();
        subjectJudge.setSubjectId(subjectId);
        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectJudge> subjectJudges = subjectJudgeService.queryByCondition(subjectJudge);
        //转换
        List<SubjectAnswerBO> subjectAnswerBOS = JudgeSubjectConverter.INSTANCE.convertJudgeToAnswerBOList(subjectJudges);
        //构造对象
        SubjectOptionBO subjectOptionBO=new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);

        return subjectOptionBO;
    }
}
